package com.example.myretrofit2.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myretrofit2.Category;
import com.example.myretrofit2.R;
import com.example.myretrofit2.common.MyApplication;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class CategoryRightAdapter extends RecyclerView.Adapter<CategoryRightAdapter.MyHolder> implements View.OnClickListener{
    List<Category> list;
    Context context;
    OnitemClickListener listener;
    public CategoryRightAdapter(List<Category> list, Context context){
        this.list=list;
       this.context=context;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context,R.layout.right_list,null);
        MyHolder myHolder = new MyHolder(view);
        view.setOnClickListener(this);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.textView.setText(list.get(position).getCategoryname());
        ImageLoader.getInstance().displayImage(MyApplication.imageUrl+list.get(position).getSrc(),holder.imageView);
        holder.itemView.setTag(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public void setListener(OnitemClickListener listener){
        this.listener = listener;
    }
    @Override
    public void onClick(View view) {
        if(listener!=null){
            Category category = (Category) view.getTag();
            listener.onItemClick(category);
        }
    }

    public class MyHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.right_tv);
            imageView = itemView.findViewById(R.id.right_iv);
        }
    }
}
